package com.laicunba.ext.web.controller.console.sys;

import java.math.BigDecimal;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.RateIncrCouponDefDO;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.RateIncrCouponDefService;

@SuppressWarnings("unchecked")
@Controller
@RequestMapping(value = "/console/ext/sys")
public class CouponCreateController extends BaseController {

	@Autowired
	private CashCouponDefService cashCouponDefService;

	@Autowired
	private RateIncrCouponDefService rateIncrCouponDefService;

	@RequestMapping(value = "/coupon_create_form.htm")
	public ModelAndView couponCreateForm(String type) {
		ModelAndView view = new ModelAndView();
		view.addObject("type", "cash_coupon".equalsIgnoreCase(type) ? "cash_coupon" : "rate_incr_coupon");
		view.setViewName("coupon_create_form.jsp");
		return view;

	}

	@ResponseBody
	@RequestMapping(value = "/create_cash_coupon_def.htm")
	public String createCashCouponDef(String name, String amount, String buyAmount, int period, int validDay,
			int totalQuantity,String type) {

		CashCouponDefDO defDO = new CashCouponDefDO();
		defDO.setName(name);
		defDO.setAmount(BigDecimal.valueOf(Double.parseDouble(amount)));
		defDO.setValidDay(validDay);
		defDO.setTotalQuantity(totalQuantity);
		JSONObject json = new JSONObject();
		if (defDO == null || buyAmount == null) {
			json.put("code", "param_error");
			return json.toJSONString();
		}
		if (defDO.getAmount() == null || defDO.getAmount().compareTo(BigDecimal.valueOf(100)) > 0
				|| defDO.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
			json.put("code", "amount_error");
			return json.toJSONString();
		}

		JSONArray conditions = new JSONArray();
		JSONObject codition1 = new JSONObject();
		conditions.add(codition1);
		codition1.put("buy_amount", BigDecimal.valueOf(Double.parseDouble(buyAmount)).toBigInteger());
		codition1.put("period", period);
		codition1.put("target_type", "CPB");
		defDO.setConditions(conditions.toJSONString());
		defDO.setAddProfit("profit".equalsIgnoreCase(type));

		json.put("code", cashCouponDefService.create(defDO) ? "SUCCESS" : "FAIL");
		return json.toJSONString();
	}

	@ResponseBody
	@RequestMapping(value = "/create_rate_incr_coupon_def.htm")
	public String createRateIncrCouponDef(String name, String amount, String buyAmount, int period, int validDay,
			int totalQuantity) {

		RateIncrCouponDefDO defDO = new RateIncrCouponDefDO();
		defDO.setName(name);
		defDO.setRate(BigDecimal.valueOf(Double.parseDouble(amount)));
		defDO.setValidDay(validDay);
		defDO.setTotalQuantity(totalQuantity);
		JSONObject json = new JSONObject();
		if (defDO == null || buyAmount == null) {
			json.put("code", "param_error");
			return json.toJSONString();
		}
		if (defDO.getRate() == null || defDO.getRate().compareTo(BigDecimal.valueOf(10)) > 0
				|| defDO.getRate().compareTo(BigDecimal.ZERO) <= 0) {
			json.put("code", "amount_error");
			return json.toJSONString();
		}

		JSONArray conditions = new JSONArray();
		JSONObject codition1 = new JSONObject();
		conditions.add(codition1);
		codition1.put("buy_amount", BigDecimal.valueOf(Double.parseDouble(buyAmount)).toBigInteger());
		codition1.put("period", period);
		codition1.put("target_type", "CPB");
		defDO.setConditions(conditions.toJSONString());

		json.put("code", rateIncrCouponDefService.create(defDO) ? "SUCCESS" : "FAIL");
		return json.toJSONString();
	}

}
